package etxt2db.main;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import etxt2db.api.ClassificationExecutor;
import etxt2db.api.ClassificationModel;
import etxt2db.api.ClassificationModelCreator;
import etxt2db.api.ClassificationModelCreator.MLTechnique;
import etxt2db.features.CharacterFeatureClassifier;
import etxt2db.features.EditableTokenFE;
import etxt2db.serialization.ClassificationModelSerializer;

public class Trainer {

	protected static int[] spl = {/*0,1,2,3,4,5,6,7,8,*/9};

	protected static MLTechnique[] mltechniques = {/*ClassificationModelCreator.MLTechnique.HMM,*/ ClassificationModelCreator.MLTechnique.MEMM/*, ClassificationModelCreator.MLTechnique.CRF, ClassificationModelCreator.MLTechnique.SVM*/};

	protected static String[] names = {/*"HMM",*/"MEMM"/*,"CRF",SVM"*/}; 

	
	public class TrainerRunnable implements Runnable {
	
		private int folder;
		private MLTechnique mltechnique;
		private String name;

		public TrainerRunnable(int folder, MLTechnique mltechnique, String name){
			
			this.folder = folder;
			this.mltechnique = mltechnique;
			this.name = name;
			
		}
		
		public void run() {
			
			ClassificationModelSerializer serial = new ClassificationModelSerializer();
			ClassificationModelCreator trainer = new ClassificationModelCreator();
			List<String> types = new ArrayList<String>();
			types.add("NATURALDISASTER");
	
			List<CharacterFeatureClassifier> listFE = new ArrayList<CharacterFeatureClassifier>();
			
			//listFE.add(new CharacterTypeFeatureClassifier());
			//listFE.add(new PatternFeatureClassifier());
			//listFE.add(new ValueCaseSensitiveFeatureClassifier());
			//listFE.add(new ValueCaseInsensitiveFeatureClassifier());
			//listFE.add(new RegexFeatureClassifier("Location|location|Location:|location:"));
			//listFE.add(new RegexFeatureClassifier("[Rr]oom"));
			//listFE.add(new ClassificationModelFeatureClassifier(serial.deserializeClassificationModel("/home/goncalo/Dropbox/Etxt2db/Etxt2db/annotators/HMMPOSTagger.ann"), "POS"));
	
			try {

				EditableTokenFE fe = new EditableTokenFE(listFE);
				
				File trainingFile = new File("/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + folder + "/trainingDirectory/");
				
				ClassificationModel model = trainer.trainMachineLearningModel(trainingFile,mltechnique,types);
				
				serial.serializeClassificationModel(model, "/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + folder + "/NaturalDisaster" + name + ".bin");

			} catch (IOException e) {
				
				System.err.println("IO EXCEPTION: " + folder + " - " + name);
				
				e.printStackTrace();
			} catch (ParseException e) {
				
				System.err.println("PARSER EXCEPTION: " + folder + " - " + name);
				
				e.printStackTrace();
			}
	
			
			
		}

	}
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws IOException, ParseException{
		// TODO Auto-generated method stub

		for (int i = 0; i < spl.length; i++) {
			
			for (int j = 0; j < names.length; j++) {
		
				new Trainer().execute(spl[i],mltechniques[j],names[j]);
				
			}
			
		}
			
	}

	private void execute(int folder, MLTechnique mltechnique, String name) {
		
		System.err.println(folder + " - " + name);
		
		
		Thread t = new Thread(new TrainerRunnable(folder, mltechnique, name));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
